<?php
/**
 * Author: wl
 * Email: 446399160@qq.com
 * DateTime: 2017/04/22 11:33
 */

namespace app\modules\v1\models;
use yii\db\ActiveRecord;
use Yii;
use yii\base\Exception;

class Merchantinfocard extends ActiveRecord{

    public static function tableName()
    {
        return '{{merchant_infor_card}}';
    }

    public function init()
    {
        parent::init();
    }
    
    public static function GetBankCode($bank_name,$bank_province,$bank_city,$bank_area)
    {
    	try {
    		$connection  = Yii::$app->db;
    		$sql0     = "select code from bank_name_code where bank_name  like '%{$bank_name}%'";
    		$command0 = $connection->createCommand($sql0);
    		$codes = $command0->queryAll();
    		if(count($codes)>0){
    			$code  = $codes[0]['code'];
    		}else{
    			return '308290003492';
    		}
    		
    		$sql     = "select distinct county_code from china_county where province='{$bank_province}' AND city='{$bank_city}'";
    		$command = $connection->createCommand($sql);
    		$county_codes = $command->queryAll();
    		$StrCountyCode = '';
    		if(count($county_codes)>0){
    			foreach ($county_codes as $county_code){
    				$StrCountyCode .= $county_code['county_code'].',';
    			}
    		}
    		$StrCountyCode = substr($StrCountyCode,0,strlen($StrCountyCode)-1);
    		$sql2 = "SELECT bank_vnion_code,bank_name from bank_vnion_code where code='{$code}' AND county_code IN ({$StrCountyCode}) LIMIT 1";
    		$command2 = $connection->createCommand($sql2);
    		$bank_vnion_codes = $command2->queryAll();
    		if(count($bank_vnion_codes)>0){
    			return $bank_vnion_codes[0]['bank_vnion_code'];
    		}else{
    			return '308290003492';
    		}
    	}catch (Exception $e) {
    		return '308290003492';
    	}
    }
    
}